package com.bw.liuyushan.liuyushan20210929.MVP.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.blankj.utilcode.util.NotificationUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.bw.liuyushan.liuyushan20210929.Entity.UserBean;
import com.bw.liuyushan.liuyushan20210929.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Model.BaseModelTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.liuyushan20210929.R;
import com.bw.liuyushan.mvp_lib.View.BaseActivity;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 接口登录 第三方登录
 */
public class UesrMainActivity extends BaseActivity<BasePresenterTest> implements IContrackTest.C_ViewTest {


    private android.widget.EditText phone;
    private android.widget.EditText numbers;
    private android.widget.Button send;
    private android.widget.EditText username;
    private android.widget.EditText password;
    private android.widget.Button login;
    private android.widget.Button goreg;
    private android.widget.ImageView qq;
    Timer timer;
    int i = 60;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what==200){
                i--;
                if (i<0){
                    send.setEnabled(true);
                    i=60;
                    timer.cancel();

                    send.setText("再次发送验证码");
                }else{
                    send.setEnabled(false);
                    send.setText(""+i);
                }
            }
            return false;
        }
    });


    @Override
    public int bindLayout() {
        return R.layout.activity_usermain;
    }

    @Override
    public void initView() {
        //链接 防内存泄露
        mPresenter=new BasePresenterTest(new BaseModelTest(),this);

        phone = (EditText) findViewById(R.id.phone);
        numbers = (EditText) findViewById(R.id.numbers);
        send = (Button) findViewById(R.id.send);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        goreg = (Button) findViewById(R.id.goreg);
        qq = (ImageView) findViewById(R.id.qq);
    }

    @Override
    public void initDate() {
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(UesrMainActivity.this).setShareConfig(config);
        qq.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                UMShareAPI.get(UesrMainActivity.this).getPlatformInfo(UesrMainActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                        ToastUtils.showShort("登陆成功");
                        Intent intent = new Intent(UesrMainActivity.this, MainActivity.class);
                        startActivity(intent);

                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {

                    }
                });

            }
        });
        goreg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UesrMainActivity.this, UserRegActivity.class);
                startActivity(intent);
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = username.getText().toString();
                String pass = password.getText().toString();
                String phones = phone.getText().toString();
                String numberss = numbers.getText().toString();
                if (phones.isEmpty()){
                    ToastUtils.showShort("手机号不能为空");
                }else{
                    if (numberss.isEmpty()){
                        ToastUtils.showShort("验证码不能为空");
                    }else{
                        if (name.isEmpty()){
                            ToastUtils.showShort("用户名不能为空");
                        }else{
                            if (pass.isEmpty()){
                                ToastUtils.showShort("密码不能为空");
                            }else{
                                mPresenter.mpLogin(name,pass);
                            }
                        }

                    }
                }

            }
        });
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(200);
                    }
                },0,1000);
                //1-9随机六位数
                int number = (int) ((Math.random()*9+1)*100000);
                //发送通知
                NotificationUtils.notify(1, new Utils.Consumer<NotificationCompat.Builder>() {
                    @Override
                    public void accept(NotificationCompat.Builder builder) {
                        builder.setSmallIcon(R.mipmap.ic_launcher);
                        builder.setContentTitle("收到一条新消息");
                        builder.setContentText("登录随机六位验证码:"+number);
                    }
                });
            }
        });
    }

    @Override
    public void ShowLogin(UserBean userBean) {
        if (userBean.getErrorCode()==0){
            ToastUtils.showShort("登陆成功");
            Intent intent = new Intent(UesrMainActivity.this, MainActivity.class);
            startActivity(intent);
        }else{
            ToastUtils.showShort("登录失败");
        }
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
